//
//  GQHPushFreightsController.m
//  Xanthium
//
//  Created by GuanQinghao on 07/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHFreightModel.h"

#pragma mark Object

#pragma mark View
#import "GQHPushFreightsView.h"

#pragma mark Controller
#import "GQHPushFreightsController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHPushFreightsController () <GQHPushFreightsViewDelegate>{
    
#pragma mark Data
    NSMutableArray *freightModelArray;
#pragma mark View
    GQHPushFreightsView *rootView;
}

@end


@implementation GQHPushFreightsController

#pragma mark --Lifecycle
- (void)loadView {
    // 添加自定义视图
    DLog();
    
    rootView = [[GQHPushFreightsView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    rootView.qh_truckModel = _qh_truckModel;
    
    freightModelArray = [NSMutableArray array];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:_qh_truckModel.qh_driverID forKey:@"driverId"];
    [parameters setObject:USER_GET_ID forKey:@"shippId"];
    [self fetchServiceDataWithParameters:parameters];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"推送货源";
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate
- (void)qh_pushFreight:(UIButton *)sender {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"shipperId"];
    GQHFreightModel *freightModel = freightModelArray[sender.tag];
    [parameters setObject:freightModel.qh_freightID forKey:@"goodsSupply"];
    [parameters setObject:_qh_truckModel.qh_driverID forKey:@"driverIds"];
    
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buGoodsDeliveryPush/1/save"];
    
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        
        if (0 == [code intValue]) {
            
            [self showToastWithText:message];
            
            [rootView.qh_tableView.mj_header beginRefreshing];
            [rootView.qh_tableView.mj_header endRefreshing];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchServiceDataWithParameters:(NSMutableDictionary *)parameters {
    
    __block int page = 1;
    
    [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buGoodsDeliveryPush/1/findAll"];
        [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
            DLog(@"%@---%@---%@",code,message,data);
            
            if (0 == [code integerValue]) {
                
                NSMutableArray *freightArray = [NSMutableArray array];
                for (NSDictionary *object in [data objectForKey:@"list"]) {
                    
                    GQHFreightModel *freightModel = [GQHFreightModel yy_modelWithDictionary:object];
                    if (freightModel) {
                        
                        [freightArray addObject:freightModel];
                    }
                }
                
                if (freightArray && freightArray.count > 0) {
                    
                    freightModelArray = freightArray;
                    rootView.qh_freightModelArray = freightArray;
                    [rootView.qh_tableView reloadData];
                }
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
        }];
        [rootView.qh_tableView.mj_header endRefreshing];
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buGoodsDeliveryPush/1/findAll"];
        [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
            DLog(@"%@---%@---%@",code,message,data);
            
            if (0 == [code integerValue]) {
                
                NSMutableArray *freightArray = [NSMutableArray array];
                for (NSDictionary *object in [data objectForKey:@"list"]) {
                    
                    GQHFreightModel *freightModel = [GQHFreightModel yy_modelWithDictionary:object];
                    if (freightModel) {
                        
                        [freightArray addObject:freightModel];
                    }
                }
                if (freightArray && freightArray.count > 0) {
                    
                    freightModelArray = freightArray;
                    rootView.qh_freightModelArray = freightArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                } else {
                    
                    [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                
                [rootView.qh_tableView.mj_footer endRefreshing];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_footer endRefreshing];
        }];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
